<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>提报查询</span>
      </div>

      <el-form
        :inline="true"
        v-show="showSearch"
        :model="queryParams"
        ref="queryForm"
        label-width="68px"
      >
        <el-form-item label="查询时间">
          <el-date-picker
            v-model="dateRange"
            type="datetimerange"
            size="small"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提报人" prop="declarerUserName">
          <el-input
            v-model="queryParams.declarerUserName"
            placeholder="请输入提报人"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select
            v-model="queryParams.state"
            clearable
            placeholder="请选择状态"
          >
            <el-option
              v-for="item in stateOptions"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['work:search:export']"
            >导出</el-button
          >
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="resList" stripe>
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">
            {{
              (queryParams.pageNum - 1) * queryParams.pageSize +
              scope.$index +
              1
            }}
          </template>
        </el-table-column>
        <el-table-column label="提报日期" prop="fillDate" />
        <el-table-column label="提报人" prop="declarerUserName" />
        <el-table-column label="项目代码" prop="projectCode" />
        <el-table-column label="开发阶段" align="center" prop="stageName" />
        <el-table-column
          label="事务种类"
          align="center"
          width="230"
          prop="officeName"
        />
        <el-table-column label="提报工时(H)" align="center" prop="workHour" />
        <el-table-column label="状态" align="center" prop="state">
          <template slot-scope="scope">
            <el-tag
              :type="
                scope.row.state === '已审核'
                  ? 'primary'
                  : scope.row.state === '未审核'
                  ? 'info'
                  : scope.row.state === '回退'
                  ? 'warning'
                  : 'danger'
              "
              disable-transitions
              >{{ scope.row.state }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="审核人" prop="checkByName" />

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="100"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 查看 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="提报日期" prop="fillDate">
          <el-date-picker
            v-model="form.fillDate"
            readonly
            type="datetime"
            placeholder="选择日期时间"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提报人员" prop="declarerUserName">
          <el-input v-model="form.declarerUserName" />
        </el-form-item>
        <el-form-item label="项目代码" prop="projectCode">
          <el-input v-model="form.projectCode" />
        </el-form-item>
        <el-form-item label="项目阶段" prop="stageName">
          <el-input v-model="form.stageName" />
        </el-form-item>
        <el-form-item label="事务种类" prop="officeName">
          <el-input v-model="form.officeName" />
        </el-form-item>
        <el-form-item label="申报工时" prop="workHour">
          <el-input v-model="form.workHour" />
        </el-form-item>
        <el-form-item label="申报备注" prop="workRemark">
          <el-input v-model="form.workRemark" type="textarea" />
        </el-form-item>
        <el-form-item label="审核状态" prop="state">
          <el-input v-model="form.state" />
        </el-form-item>
        <el-form-item label="审核人" prop="checkByName">
          <el-input v-model="form.checkByName" />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listSubmitQuery } from '@/api/work/search'

export default {
  name: 'Search',
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        declarerUserName: '',
        state: ''
      },
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: false,
      // 总条数
      total: 0,
      // 提报查询列表
      resList: [],
      // 日期选择
      pickerOptions: {
        shortcuts: [
          {
            text: '当天',
            onClick(picker) {
              const end = new Date(
                new Date(new Date().toLocaleDateString()).getTime() +
                  24 * 60 * 60 * 1000 -
                  1
              )
              const start = new Date(
                new Date(new Date().toLocaleDateString()).getTime()
              )
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      // 日期范围
      dateRange: '',
      // 状态选择
      stateOptions: ['未审核', '已审核', '回退', '未提报'],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {}
    }
  },
  methods: {
    /** 初始化 */
    getList() {
      if (this.dateRange && this.dateRange.length > 0) {
        this.dateRange = this.dateRange.map((e) => this.parseTime(e))
        this.loading = true
        listSubmitQuery(
          this.addDateRange(this.queryParams, this.dateRange)
        ).then((response) => {
          this.resList = response.rows
          this.total = response.total
          this.loading = false
        })
      } else {
        this.$message.warning('提示：必须输入时间，否则没有数据显示')
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = ''
      this.resetForm('queryForm')
      // 清空table
      this.resList = []
    },
    /** 详情 */
    handleView(row) {
      if (row.state.startsWith('未提报')) {
        this.$message.warning('提示：人员未提报，暂无数据')
      } else {
        this.form = {}
        this.resetForm('form')
        let copy = Object.assign({}, row) //row是所编辑本行内数据对象
        this.form = copy
        this.open = true
        this.title = this.form.declarerUserName + '申报详情'
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'work/search/export',
        {
          ...this.queryParams
        },
        `提报查询_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
